package com.example.liumengqiang.mvvmoneproject.one;

import android.content.Context;
import android.databinding.BaseObservable;

import android.databinding.ObservableField;

import java.lang.ref.WeakReference;


public class TestOneViewModel  extends BaseObservable{

    //String 类型
    public static ObservableField<String> textObservable = new ObservableField<>();
    //引用数据类型
    public static ObservableField<TestOneModel> testOneObservable = new ObservableField<>();

    //接受接口实例
    private WeakReference<TestOneNagivator> mNagivator;

    private Context context;

    public TestOneViewModel(Context context) {
        this.context = context;
    }

    public void onStart() {
        textObservable.set("hello user");
        getDataFromServer();

    }

    /**
     * 获取数据从服务端
     */
    public void getDataFromServer() {
        //TODO 获取到数据之后，在设置textObservable的值，然后通过mNagivator回调给UI层，做进一步需求操作
        //假设已经从服务器获取到了数据
        TestOneModel testOneModel = new TestOneModel("欢迎来到", "MVVM的世界");
        testOneObservable.set(testOneModel);
    }

    public void setNagivator(TestOneNagivator nagivator) {
        this.mNagivator = new WeakReference<>(nagivator);
    }

    public void onTextClick() {
        if(mNagivator != null && mNagivator.get() != null) {
            //回调给UI层
            mNagivator.get().onClick();
        }
    }


}
